﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TM.Plugable;
using TM.Data;
using TM.Data.Contracts;
using TM.Data.ViewModels.VmMapper;
using DevExpress.XtraScheduler;
using TM.Data.ViewModels;

namespace TM.ProjectKeeper
{
    public partial class ScheduleEditorControl : UserControl, IProjectEditor
    {
        private bool _isDirty;
        public ScheduleEditorControl()
        {
            InitializeComponent();
            this.splitContainerControl1.Dock = DockStyle.Fill;
            this.schedulerControl1.Dock = DockStyle.Fill;    
        }

        public IVmMapper VmMapper { get; private set; }
        public IUnitOfWork UoW { get; private set; }
        public bool IsDataLoaded { get; private set; }
        public bool IsDirty
        {
            get { return _isDirty; }
            private set
            {
                if (_isDirty != value)
                {
                    InvokeDataChanged(value);
                }
                _isDirty = value;
            }
        }

        public Guid ProjectId { get; private set; }

        public void SetIoC(IIoCParams ioCParams, IVmMapper vmMapper)
        {
            VmMapper = vmMapper;
            UoW = ioCParams.UnitOfWork;
        }
        public object Session { get; private set; }
        public void LoadProjectInformation(Guid projectId)
        {
            ProjectId = projectId;

            var result = VmMapper.ExtractFromDboAsDataSet(false, projectId);
            if (result != null && result.Projects.Count > 0)
            {                
                SchedulerStorage schedulerStorage = schedulerControl1.Storage;
                AppointmentMappingInfo appointmentMappings = schedulerStorage.Appointments.Mappings;

                appointmentMappings.AppointmentId = "ID";
                appointmentMappings.Description = "DescDetails";
                appointmentMappings.Start = "StartDate";
                appointmentMappings.End = "EndDate";
                appointmentMappings.Subject = "Name";
                appointmentMappings.PercentComplete = "PercentCompleted";
                appointmentMappings.ResourceId = "Resources";
                //appointmentMappings.Status = "Status";
                schedulerStorage.Appointments.CommitIdToDataSource = false;

                // 3) Bind scheduler to data
                schedulerStorage.Appointments.DataSource = result.ProjectsDataSet.Tables["Tasks"];
                // schedulerStorage.Appointments.DataSource = task;
                if (schedulerStorage.Appointments.Count > 0)
                    schedulerControl1.Start = schedulerStorage.Appointments[0].Start;
            
            }
            result.ProjectsDataSet.Tables["Tasks"].TableNewRow += new DataTableNewRowEventHandler(ScheduleEditorControl_TableNewRow);
            result.ProjectsDataSet.Tables["Tasks"].RowDeleted += new DataRowChangeEventHandler(ScheduleEditorControl_RowDeleted);
            result.ProjectsDataSet.Tables["Tasks"].RowChanged += new DataRowChangeEventHandler(ScheduleEditorControl_RowChanged);
            IsDataLoaded = true;
        }
        public event ProjectDataIsChangedHandler ProjectDataIsChanged;
        void ScheduleEditorControl_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            this.IsDirty = true;
        }

        void ScheduleEditorControl_RowDeleted(object sender, DataRowChangeEventArgs e)
        {
            this.IsDirty = true;
        }

        void ScheduleEditorControl_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            this.IsDirty = true;
        }
        public void RemoveCurrentTask()
        {
            throw new NotImplementedException();
        }

        public void CreateNewTask()
        {            
            Appointment ap = new Appointment(DateTime.Today,DateTime.Today);
           // apt.CustomFields["AppointmentCustomField"] = objectToStoreInACustomField;
            schedulerControl1.ShowEditAppointmentForm(ap);
        }
        
        public void Release()
        {
            throw new NotImplementedException();
        }

       public void Save()
        {
            var tasks = VmMapper.ExtractTaskDboFormSessionInAppointment(Session as Session);
            UoW.Task.Update(tasks);
        }

       private void InvokeDataChanged(bool isDirty)
       {
           if (ProjectDataIsChanged != null)
           {
               ProjectDataIsChanged.Invoke(this, isDirty);
           }
       }

       
    }
}
